/**
 * Connect to MongoDB via mongoose driver for non-file operation purpose
 */
//=====================================================================
global.mongoose = require('mongoose');

//var mongoUri = 'mongodb://' + MongoDBHostIp + '/66xiaoqu';
mongoose.connect(mongoUri);

var db = mongoose.connection;

db.on('connected', function () {
    logger.info('Connected to MongoDB via mongoose with - ' + mongoUri + '...!');

});

db.on('error', function (err) {
    logger.info('unable to connect to database at ' + mongoUri + ':' + err.message);
});


/**
 * Load and initiate all of the scheams of mongoDB
 */
require('../services/models/mongodb/user-mongo');
require('../services/models/mongodb/group-mongo');
require('../services/models/mongodb/question-mongo');
require('../services/models/mongodb/community-mongo');
require('../services/models/mongodb/relationShip-mongo');
require('../services/models/mongodb/masterData-mongo');
require('../services/models/mongodb/system-mongo');
require('../services/models/mongodb/activity-mongo');
require('../services/models/mongodb/market-mongo');

/*  宝宝树66小区二次改造    add   guo.xiaomin  20170921   start   */
require('../services/models/mongodb/mtOrder-mongo');
/*  宝宝树66小区二次改造    add   guo.xiaomin  20170921   end   */

require('../services/models/mongodb/backoffice/backOffice-audit-mongo');
require('../services/models/mongodb/backoffice/backOffice-rest-mongo');
require('../services/models/mongodb/backoffice/backOffice-adminUser-mongo');
require('../services/models/mongodb/backoffice/backOffice-lottery-mongo');
require('../services/models/mongodb/backoffice/backOffice-userServiceChain-mongo');
require('../services/models/mongodb/userDeviceProfile-mongo');

require('../services/models/mongodb/mobile/shares-mongo');
require('../services/models/mongodb/iwant-mongo');

// 心愿mongo
require('../services/models/mongodb/weixin/wish-mongo');

/**
 * Connection to MySQL
 */
//=====================================================================
if (MySQLFlag == 'Yes') {
    var mysql = require('mysql');
    //global.mysqlClient = mysql.createConnection({
    //    host: MySQLHostIp,
    //    user: 'root',
    //    password: '123456'
    //});
    //mysqlClient.connect(function (err, results) {
    //    if (err) {
    //        logger.info('Connect to mySQL database failed:' + err.message);
    //    } else {
    //        logger.info('Connected to mySQL database - ' + host + '!');
    //    }
    //});

    global.mysqlPool = mysql.createPool(mysqlPoolOption);
    global.mysqlBIPool = mysql.createPool(mysqlBIPoolOption);

    mysqlPool.on('connection', function (connection) {
    });

    mysqlPool.on('enqueue', function () {
        //console.log('Waiting for available connection slot');
    });
}


